package spittr.web;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;
import spittr.data.Spittle;
import spittr.data.SpittleRepository;

@Controller
@RequestMapping("/spittles")
public class SpittleController {
	private SpittleRepository spittleRepository;

	@Autowired
	public SpittleController(SpittleRepository spittleRepository) {
		System.out.println("AutoWired SpittleController");
		this.spittleRepository = spittleRepository;
	}

	@RequestMapping(value = "/{spittlesId}_{spittlesCount}", method = RequestMethod.GET)
	public String spittle(@PathVariable("spittlesId") long max, @PathVariable("spittlesCount") int count, Model model) {

		List<Spittle> list = spittleRepository.findSpittles(max, count);
		// model.addAttribute( spittleRepository.findSpittles(Long.MAX_VALUE,
		// 20));
		// System.out.println("list == null?" + (list == null));
		model.addAttribute("spittleList", list);

		return "spittles";
	}

	@RequestMapping(value = "/reg", method = RequestMethod.GET)
	public String spittleRegister(Model model) {
		
		model.addAttribute("spitter", new Spittle());
		return "reg";
	}

	@RequestMapping(value = "/reg", method = RequestMethod.POST)
	public String spittleDoRegister(
		HttpServletRequest request,
			@RequestPart("picture") MultipartFile picture,
			@Valid Spittle spittle, Errors errors) throws IOException {
		
		String basePath = request.getServletContext().getRealPath("/");
		
		System.out.println("path=" + request.getServletContext().getRealPath("/"));
		System.out.println("picture size=" + picture.getBytes().length + ", name = " + picture.getOriginalFilename());

		picture.transferTo(new File( basePath + "/uploads/" + picture.getOriginalFilename()));

		System.out.println("has error?" + errors.hasErrors());
		if (errors.hasErrors()){
			return "reg";
		}
		System.out.println("reg post=" + spittle.getId());
		System.out.println("reg post=" + spittle.getMessage());

		spittleRepository.addSpittles(spittle);
		return "redirect:/spittles/90_100";
	}

	@RequestMapping(value = "/profile/{spittleId}", method = RequestMethod.GET)
	public String spittleProfile(@PathVariable("spittleId") Long Id, Map<String, Object> map) {

		System.out.println("hee");
		Spittle spittle = spittleRepository.getSpittle(Id);

		map.put("spittle", spittle);

		return "profile";
	}
}
